<?php
declare(strict_types=1);

namespace app\index;

use lib\ExtiStorage;
use sys\Log;

class BootStarup {
    public static int $start_time;
    /**
     * 当工作进程启动的时候执行该方法.
     * @param int $serveId 服务器ID
     * @param int $workerId 工作进程ID: 0~n
     */
    public static int $serveId;
    public static int $workerId; 
    public static string $nodeKey;
    public static string $sid_36;
    public static int $sid;

    protected static array $sid_map = [
        # 0号服务器 sid
        0=>[
            0=>0,
            1=>1,
            2=>2,
            3=>3,
            4=>4,
        ]
    ];

    public static function onWorkerStart(int $serverId, int $workerId) : void {
        static::$start_time = time();
        static::$workerId = $workerId;
        static::$serveId = $serverId;

        static::$sid = static::$sid_map[$serverId][$workerId];
        $sid = base_convert((string)static::$sid, 10, 36);
        static::$sid_36 = strtoupper(str_pad($sid, 3, '0', STR_PAD_LEFT));

        //$result = ExtiStorage::delete('/score_thumbs/20220224/bc89e5aa5244d07cea5daadade7a86db.jpeg');
        //Log::console(json_encode($result, JSON_UNESCAPED_UNICODE), 'DEBUG');

/*
DELETE FROM tp_nb_goods_documents WHERE mid = 103;
ALTER TABLE tp_nb_goods_documents AUTO_INCREMENT = 1;

DELETE FROM el_components_changes WHERE mid = 103;
ALTER TABLE el_components_changes AUTO_INCREMENT = 1;

DELETE FROM tp_nb_goods_order WHERE mid = 103;
ALTER TABLE tp_nb_goods_order AUTO_INCREMENT = 1;

DELETE FROM tp_nb_goods_order_flow WHERE mid = 103;
ALTER TABLE tp_nb_goods_order_flow AUTO_INCREMENT = 1;

DELETE FROM el_components_changes WHERE mid = 103;
ALTER TABLE el_components_changes AUTO_INCREMENT = 1;

DELETE FROM el_sync WHERE mid = 103;
ALTER TABLE el_sync AUTO_INCREMENT = 1;

DELETE FROM tp_nb_goods_cashout_request WHERE mid = 103;
ALTER TABLE tp_nb_goods_cashout_request AUTO_INCREMENT = 1;

DELETE FROM tp_nb_goods_store_map WHERE mid = 103;
ALTER TABLE tp_nb_goods_store_map AUTO_INCREMENT = 1;

DELETE FROM tp_nb_goods_order_distribution WHERE mid = 103;
ALTER TABLE tp_nb_goods_order_distribution AUTO_INCREMENT = 1;

DELETE FROM tp_nb_goods_messages WHERE mid = 103;
ALTER TABLE tp_nb_goods_messages AUTO_INCREMENT = 1;

DELETE FROM  tp_nb_goods_shift WHERE mid = 103;
ALTER TABLE tp_nb_goods_shift AUTO_INCREMENT = 1;

UPDATE el_dataversion SET version=1 WHERE mid = 103;

UPDATE tp_nb_goods_rack SET num = 0 WHERE mid = 103;

UPDATE tp_nb_merchant_goods SET amount=0,amount_online=0,amount_member=0, shift_id=0 where mid = 103;

*/
        if($workerId == 0){
            echo "Worker BootStrap {$serverId}, {$workerId} ".\lib\Utils::genOrderNo('TEST')."\n";
            echo "SM3 = ". \mexti\Crypto::sm3('xie.996') . "\n";
        }
        

        # 初始化商户数据
        \lib\Merchant::init($workerId);
        
        # 初始化用户缓存
        $cachefile = APP_ROOT .'/var/cache/'.str_pad(strval($workerId), 2, '0', STR_PAD_LEFT).'.cache2';
        \lib\LoginTokens::start($cachefile);
    }

    /**
     * 当工作进程退出的时候执行该方法.
     * @param int $serverId 服务器ID
     * @param int $workerId 工作进程ID: 0~n
     */
    public static function onWorkerShutdown(int $serverId, int $workerId): void
    {
        \lib\LoginTokens::shutdown();
        echo "Worker Stop {$serverId}, {$workerId}\n";
    }
}
